package com.cloud.lan.config.exception;

import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.base.exceptions.BaseException;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@ControllerAdvice
public class WebExceptionHandler {

//    @ExceptionHandler
//    public String testError(ArithmeticException e, HttpServletRequest request) {
//        request.setAttribute("javax.servlet.error.status_code", 500);
//        request.setAttribute("code", 66);
//        request.setAttribute("message", "HTTP 500内部错误");
//        return "forward:/error";
//    }

//    @ExceptionHandler
//    public String methodArgumentNotValid(BindException e) {
//        log.error("参数校验失败", e);
//        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
//        StringBuilder errorMessage = new StringBuilder();
//        for (int i = 0; i < allErrors.size(); i++) {
//            ObjectError error = allErrors.get(i);
//            errorMessage.append(error.getDefaultMessage());
//            if (i != allErrors.size() - 1) {
//                errorMessage.append(", ");
//            }
//        }
//        return generateErrorInfo(1, errorMessage.toString(), HttpStatus.BAD_REQUEST.value());
//    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public BaseResponse unknownException(Exception exception) {
        log.error("server error: ", exception);
        //通知错误消息至钉钉群
//        TalkExceptionUtil.DingTalk("异常监控MINI", ExceptionUtil.stacktraceToString(exception));
        //通知错误消息至微信
//        TalkExceptionUtil.WeChat(ExceptionUtil.stacktraceToString(exception));
        return BaseResponse.error("系统错误");
    }

    /**
     * 生成错误信息, 放到 request 域中.
     *
     * @param code       错误码
     * @param message    错误信息
     * @param httpStatus HTTP 状态码
     * @return SpringBoot 默认提供的 /error Controller 处理器
     */
    private String generateErrorInfo(int code, String message, int httpStatus) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        request.setAttribute("code", code);
        request.setAttribute("message", message);
        request.setAttribute("javax.servlet.error.status_code", BaseResponse.ERROR);
        return "forward:/error";
    }

//    private String generateErrorInfo(int code, String message) {
//        return generateErrorInfo(code, message, HttpStatus.INTERNAL_SERVER_ERROR.value());
//    }
//
//    @ExceptionHandler(BaseException.class)
//    public BaseResponse handleException(BaseException e) {
//        log.error("自定义通用异常:", e);
//        return BaseResponse.error();
//    }

    /**
     * 处理业务异常
     * @author ErKang
     * @return com.cloud.lan.entity.base.BaseResponse
     * 2021/12/23 17:05
     */
    @ResponseBody
    @ExceptionHandler(value = BaseException.class)
    public BaseResponse<Object> bizExceptionHandler(BaseException e) {
        log.error("系统抛出 RuntimeException:", e);
        return BaseResponse.error(e.getCode(), e.getMsg());
    }

    /**
     * 处理空指针的异常
     * @author ErKang
     * @return com.cloud.lan.entity.base.BaseResponse
     * 2021/12/23 17:05
     */
    @ResponseBody
    @ExceptionHandler(value = NullPointerException.class)
    public BaseResponse<Object> exceptionHandler(NullPointerException e) {
        log.error("业务空指针 异常 :", e);
        return BaseResponse.errorNull();
    }
}
